<template>
  <div class="form">
    <el-text>注册</el-text>
    <el-form :model="form" label-width="auto" style="max-width: 600px;margin: auto;">
      <el-form-item>
        <el-button @click="back">返回登录</el-button>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.gender" placeholder="请选择性别">
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password" />
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input type="password" v-model="form.confirmPassword" />
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
            v-model="form.birth"
            placeholder="请选择日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">注册</el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script  setup>

import {reactive, ref} from "vue";
import loginApi from "@/apis/login";
import { ElNotification } from "element-plus";
import { useRouter } from "vue-router"

const router = useRouter();

let form = reactive({
  name: '',
  phone: '',
  password: '',
  confirmPassword: '',
  gender: '',
  birth: ''
})
// let response = ref('');

const onSubmit = async (form) => {
  let data = ref('');
  if(form.password === form.confirmPassword){
    data = {
      name: form.name,
      phone: form.phone,
      password: form.password,
      gender: form.gender,
      birth: form.birth
    }
  }

  // console.log(data)
  try{
    await loginApi.patientRegister(data);
    successNotify()
    await router.push({name: 'Login'});
  }catch (e){
    errorNotify()
  }
}

const reset = () => {
  form.name = '';
  form.phone = '';
  form.password = '';
  form.confirmPassword = '';
  form.gender = '';
  form.birth = '';
}

const back = () =>{
  router.push({name: 'Login'});
}

const successNotify = () => {
  ElNotification({
    title: '成功提示',
    message: '注册成功',
    type: 'success',
  })
}

const errorNotify = () => {
  ElNotification({
    title: '错误提示',
    message: '注册失败',
    type: 'error',
  })
}
</script>

<style>

</style>
